﻿var generateAsteroid = function() {
	var size = Math.random() * 50 + 25;
	var life = 10;
	var angle = 0;
	var rotation = Math.random() * 6 - 3;
	
	if(Math.random() < 0.5) {
		var x = Math.random() * c.canvas.width;
		var y = Math.random() < 0.5 ? -size / 2 : c.canvas.height + size / 2;
	} else {
		var x = Math.random() < 0.5 ? -size / 2 : c.canvas.width + size / 2;
		var y = Math.random() * c.canvas.height;
	}
	
	var vx = Math.random() * 6 - 3;
	var vy = Math.random() * 6 - 3;
	return new asteroid(size, life, angle, rotation, x, y, vx, vy);
};

var gameLoop = function() {
	if(gameRunning) {
		logic();
		items();
		draw();
		++ticks;
	}
};
//http://www.tayloredmktg.com/rgb/

window.onload = function() {
	ships.push(new ship());
	explosionImg = document.createElement('img');
	asteroidImg = document.createElement('img');
	implosionImg = document.createElement('img');
	shiposionImg = document.createElement('img');
	explosionImg.src = 'explosion.png';
	asteroidImg.src = 'asteroid.png';
	implosionImg.src = 'implosion.png';
	shiposionImg.src = 'shiposion.png';
	var count = 4;
	
	shiposionImg.onload = implosionImg.onload = explosionImg.onload = asteroidImg.onload = function() {
		--count;
		
		if(count === 0)
			loop = setInterval(gameLoop, 40);
	};
	
	socketSetup();
	c.canvas.addEventListener('keydown', keyboard.press, false);
	c.canvas.addEventListener('keyup', keyboard.release, false);
	c.canvas.addEventListener('mouseover', handlefocus, false);
	c.canvas.setAttribute('tabindex', '0');
	c.canvas.focus();
};